//
//  NSTimer+Weak.m
//  runtimerDemon
//
//  Created by Feng on 2021/9/7.
//

#import "NSTimer+weak.h"
#import <objc/runtime.h>
#import "NSObject+Swizzing.h"
#import "FengWeakProxy.h"

@implementation NSTimer (Weak)

+(void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        @autoreleasepool {
            [self swizzleWithSysMethod:@selector(scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:) swizzledMethod:@selector(scheduledSafeTimerWithTimeInterval:target:selector:userInfo:repeats:)];
            [self swizzleWithSysMethod:@selector(timerWithTimeInterval:target:selector:userInfo:repeats:) swizzledMethod:@selector(timerSafeWithTimeInterval:target:selector:userInfo:repeats:)];
        }
    });
}

+ (NSTimer *)scheduledSafeTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo {
    return [self scheduledSafeTimerWithTimeInterval:ti target:[FengWeakProxy proxyWithTarget:aTarget] selector:aSelector userInfo:userInfo repeats:yesOrNo];
}

+ (NSTimer *)timerSafeWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo {
    return [self timerSafeWithTimeInterval:ti target:[FengWeakProxy proxyWithTarget:aTarget] selector:aSelector userInfo:userInfo repeats:yesOrNo];
}

@end

